package org.asiainfo.file.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import org.asiainfo.common.core.exception.ServiceException;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.file.service.FileStrategy;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.file.service.AttactmentInfoService;
import org.asiainfo.file.domain.vo.AttactmentInfoVo;
import org.asiainfo.file.domain.bo.AttactmentInfoBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 附件详细信息
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attactmentInfo")
public class AttactmentInfoController extends BaseController {

    private final AttactmentInfoService attactmentInfoService;

    /**
     * 分页查询
     *
     * @param attactmentInfoBo 筛选条件
     * @param pageQuery        分页对象
     * @return 查询结果
     */
    @SaCheckPermission("file:attactmentInfo:list")
    @GetMapping("/list")
    public TableDataInfo<AttactmentInfoVo> list(AttactmentInfoBo attactmentInfoBo, PageQuery pageQuery) {
        return attactmentInfoService.queryPageList(attactmentInfoBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param attactmentInfoId 主键
     * @return 单条数据
     */
    @SaCheckPermission("file:attactmentInfo:query")
    @GetMapping("/{attactmentInfoId}")
    public R<AttactmentInfoVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("attactmentInfoId") Long attactmentInfoId) {
        return R.ok(attactmentInfoService.queryById(attactmentInfoId));
    }

    /**
     * 新增数据
     *
     * @param attactmentInfoBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("file:attactmentInfo:add")
    @Log(title = "附件详情", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AttactmentInfoBo attactmentInfoBo) {
        return toAjax(attactmentInfoService.insertByBo(attactmentInfoBo));
    }

    /**
     * 编辑数据
     *
     * @param attactmentInfoBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("file:attactmentInfo:edit")
    @Log(title = "附件详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AttactmentInfoBo attactmentInfoBo) {
        return toAjax(attactmentInfoService.updateByBo(attactmentInfoBo));
    }

    /**
     * 删除数据
     *
     * @param attactmentInfoIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("file:attactmentInfo:edit")
    @Log(title = "附件详情", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{attactmentInfoIds}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long[] attactmentInfoIds) {
        return toAjax(attactmentInfoService.deleteWithValidByIds(List.of(attactmentInfoIds)));
    }

    /**
     * 附件上传
     *
     * @param file             上传文件
     * @param attactmentInfoBo 附件信息
     * @return 上传结果
     */
    @PostMapping("/upload")
    public R<AttactmentInfoVo> upload(@RequestParam("file") MultipartFile file, AttactmentInfoBo attactmentInfoBo) {
        try {
            if (ObjectUtil.isNull(file) || file.isEmpty()) {
                throw new ServiceException("文件不能为空");
            }
            return R.ok(FileStrategy.uploadFile(attactmentInfoBo.getGrantType(), attactmentInfoBo, file));
        } catch (Exception ignored) {
            throw new ServiceException("附件上传失败");
        }
    }
}

